//package com.example.config;
//
//import lombok.extern.slf4j.Slf4j;
//import org.apache.catalina.connector.Connector;
//import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
//import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
//import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
//import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
//import org.springframework.context.ApplicationListener;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.event.ContextClosedEvent;
//
//import java.util.concurrent.Executor;
//import java.util.concurrent.ThreadPoolExecutor;
//import java.util.concurrent.TimeUnit;
//
///**
// * 官方讨论帖
// * https://github.com/spring-projects/spring-boot/issues/4657
// * <p>
// * Spring Boot1.X Tomcat容器优雅停机
// * https://juejin.im/post/5b023d4c6fb9a07aa542988c
// * <p>
// * SpringBoot 1.X 优雅停机 ( shutdown gracefully )
// * https://blog.csdn.net/qijiqiguai/article/details/78805838
// *
// * @author: James.H.Fu
// * @description:
// * @date: Created in 13:28 2018-05-24
// * @modified By:
// */
//@Configuration
//public class ShutdownConfig {
//    /**
//     * 用于接受shutdown事件
//     *
//     * @return
//     */
//    @Bean
//    public GracefulShutdown gracefulShutdown() {
//        return new GracefulShutdown();
//    }
//
//    /**
//     * 用于注入 connector
//     *
//     * @return
//     */
//    @Bean
//    public EmbeddedServletContainerCustomizer tomcatCustomizer() {
//        return new EmbeddedServletContainerCustomizer() {
//            @Override
//            public void customize(ConfigurableEmbeddedServletContainer container) {
//                if (container instanceof TomcatEmbeddedServletContainerFactory) {
//                    ((TomcatEmbeddedServletContainerFactory) container).addConnectorCustomizers(gracefulShutdown());
//                }
//            }
//        };
//    }
//
//    @Slf4j
//    private static class GracefulShutdown implements TomcatConnectorCustomizer, ApplicationListener<ContextClosedEvent> {
//
//        private volatile Connector connector;
//        private final int waitTime = 120;
//
//        @Override
//        public void customize(Connector connector) {
//            this.connector = connector;
//        }
//
//        @Override
//        public void onApplicationEvent(ContextClosedEvent event) {
//            this.connector.pause();
//            Executor executor = this.connector.getProtocolHandler().getExecutor();
//            if (executor instanceof ThreadPoolExecutor) {
//                try {
//                    ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
//                    log.info("shutdown start");
//                    threadPoolExecutor.shutdown();
//                    log.info("shutdown end");
//                    if (!threadPoolExecutor.awaitTermination(waitTime, TimeUnit.SECONDS)) {
//                        log.info("Tomcat 进程在{}秒内无法结束，尝试强制结束", waitTime);
//                        threadPoolExecutor.shutdownNow();
//                        log.info("强制结束shutdownNow已执行", waitTime);
//                    }
//                    log.info("shutdown success");
//                } catch (InterruptedException ex) {
//                    Thread.currentThread().interrupt();
//                }
//            }
//        }
//    }
//}
